package com.luck.easyfoodbatch.mvp.mine;

import android.content.Context;
import android.support.annotation.Nullable;

import com.luck.easyfoodbatch.callback.JsonCallback;
import com.luck.easyfoodbatch.entity.Product;
import com.luck.easyfoodbatch.mvp.model.AddressModel;
import com.luck.easyfoodbatch.mvp.model.EditGoods;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.cache.CacheMode;
import com.lzy.okhttputils.model.HttpParams;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.mvp.mine
 * email：893855882@qq.com
 * data：16/11/7
 */
public class EditGoods2Presenter implements EditGoods2Model {

    private final String TAG = getClass().getSimpleName();
    private EditGoods2View editGoods2View;
    private Context mContext;

    public EditGoods2Presenter(EditGoods2View editGoods2View, Context mContext) {
        this.editGoods2View = editGoods2View;
        this.mContext = mContext;
    }


    @Override
    public void requestEditGoods(int type, Product product) {
        HttpParams params = new HttpParams();
        params.put("gid", product.getGid());
        if (type == 0) {
            // 修改商品参数
            params.put("burden_sheet", product.getBurden_sheet());
            params.put("save_way", product.getSave_way());
            params.put("pd", "");
            params.put("expried_time", product.getExpried_time());
            params.put("food_additives", product.getFood_additives());
            params.put("net_content", "");
            params.put("pack_way", product.getPack_way());
            params.put("taste", product.getTaste());
            params.put("product_name", product.getProduct_name());
            params.put("brand", product.getBrand());
            params.put("series", product.getSeries());
            params.put("bar_code", product.getBar_code());
            params.put("factory_name", product.getFactory_name());
            params.put("factory_address", product.getFactory_address());
            params.put("factory_phone", product.getFactory_phone());
        } else if (type == 1) {
            params.put("content", product.getContent());
        } else if (type == 2) {
            params.put("license_path", product.getLicense_path());
            params.put("pass_customs_path", product.getPass_customs_path());
            params.put("bill_path", product.getBill_path());
            params.put("health_detection_path", product.getHealth_detection_path());
        }
        OkHttpUtils.post(HttpUtil.EDIT_GOODS2)
                .tag(this)
                .cacheMode(CacheMode.DEFAULT)
                .cacheKey(TAG)
                .params(params)
                .execute(new JsonCallback(mContext, EditGoods.class) {

                    @Override
                    public void onResponse(boolean isFromCache, Object data, Request request, @Nullable Response response) {
                        editGoods2View.editGoods2Success((EditGoods) data);
                        editGoods2View.hideLoading();
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                        editGoods2View.hideLoading();
                        editGoods2View.httpStatusError();
                    }
                });
    }
}
